export const ContentType = {
    Json: 'application/json',
    Form: 'application/x-www-form-urlencoded'
};

export const BodyType = {
    URLSearchParams: 'URLSearchParams',
    FormData: 'FormData',
    Blob: 'Blob',
    ArrayBuffer: 'ArrayBuffer',
    ArrayBufferView: 'ArrayBufferView',
    String: 'String',
    Json: 'String'
};

export function createBody(parama, method, contentType) {
    let dict = parama || {};
    if (Object.keys(dict).length < 1) {
        return undefined;
    }

    let abodyType = contentType || BodyType.FormData;
    if (method.toUpperCase() === 'GET') {
        abodyType = BodyType.URLSearchParams;
    }
    switch (abodyType) {
        case BodyType.FormData:
            let abody = new FormData();
            for (let one in dict) {
                abody.append(one, dict[one]);
            }
            return abody;
        case BodyType.URLSearchParams:
            if (!dict) {
                return ''
            }
            let bbody = new URLSearchParams();
            for (let one in dict) {
                bbody.append(one, dict[one]);
            }

            return bbody.toString();
        case BodyType.Json:
            return JSON.stringify(dict)

    }
    return undefined;
}


export class mo {
    static parser(result) {
        let {code, data, message} = result
        if (code === 200) {
            return {code, data, message}
        }
        return {code: code || 400, data: undefined, message: message || '未能获取数据'}
    }

    static async fetch(info) {
        let {url, method, data, headers, result, delay, parser} = info;
        method = method || 'get';
        method = method.toUpperCase();
        parser = parser || mo.parser

        headers = headers || {};
        let contentType = headers["content-type"];
        if (!contentType) {
            contentType = ContentType.Form
            headers["content-type"] = contentType
        }
        data = createBody(data, method, contentType)
        let send = {
            method: method,
            mode: 'cors'
        }
        if (method === 'GET') {
            if (data) {
                url = url + data
            }
        } else {
            send.body = data
        }
        send.headers = headers
        return new Promise(function (resolve, reject) {
            if (result) {
                let delayTime = delay || 0
                setTimeout(function () {
                    resolve({code: 200, data: result, message: '数据请求成功'})
                }, delayTime)
                return
            }
            if (!url) {
                console.error('mojson 缺少url')
                reject({message: '缺少url'})
                return
            }
            fetch(url, send)
                .then(function (response) {
                    return response.json();
                })
                .then(function (json) {
                    resolve(parser(json))
                }).catch(function (error) {
                resolve({code: '400', data: undefined, message: '数据链接失败'})
            })
        });
    }
}


